home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /** header ******************************************************************/
-
- /*
- * $Source$
- * $Revision$
- * $Date$
- * $Author$
- *
- * creator: Brett Bainter
- *
- * purpose:
- * mixed model program demonstrating
- * ...how to get continuous pointer motion events for a gl widget.
- * ...how to get keyboard events for a gl widget in a widget heirarchy.
- * ...how to map most keyboard keys to an appropriate string rep.
- *
- * compiling:
- * cc -float -prototypes -O kbexpl.c -o kbexpl \
- * -s -lXirisw -lXm_s -lXt_s -lgl_s -lX11_s -lm -lc_s -lPW
- *
- * operating:
- * This program follows the correct motif style guide by forcing the
- * user to click in the gl widget with the left mouse button for it
- * to get keyboard focus. Tab keys will be lost though as motif uses
- * these to move from tab group to tab group.
- *
- */
-
- /** notes *******************************************************************/
- /** includes ****************************************************************/
-
- #include <stdio.h> /* standard */
- #include <Xm/Xm.h> /* for motif */
- #include <Xm/Form.h> /* motif widget */
- #include <Xm/Frame.h> /* motif widget */
- #include <Xm/PushB.h> /* motif widget */
- #include <Xm/RowColumn.h> /* motif widget */
- #include <Xm/Separator.h> /* motif widget */
- #include <X11/Xirisw/GlxMDraw.h> /* gl widget */
-
- /** defines *****************************************************************/
-
- /* c environment */
- #define global
-
- /* colors */
- #define RGB_BLACK 0x00000000
- #define RGB_RED 0x000000FF
- #define RGB_GREEN 0x0000FF00
- #define RGB_BLUE 0x00FF0000
-
- /** typedefs ****************************************************************/
- /** prototypes **************************************************************/
-
- extern void main(int argc, char *argv[], char *envp[]);
-
- /* setup */
- static void install_colormaps(Widget top_level, Widget glw);
-
- /* mixed model support */
- static void cb_gl_expose(Widget w, XtPointer client_data, XtPointer call_data);
- static void cb_gl_resize(Widget w, XtPointer client_data, XtPointer call_data);
- static void cb_gl_ginit(Widget w, XtPointer client_data, XtPointer call_data);
- static void cb_gl_input(Widget w, XtPointer client_data, XtPointer call_data);
-
- /* callbacks (misc) */
- static void cb_quit(Widget w, XtPointer client_data, XtPointer call_data);
-
- /* etc */
- static void draw_frame(Bool do_clear, Bool do_swap);
- static void xbutton_print(XEvent *event);
- static char *keycode_to_string(XEvent *event);
-
- /** variables ***************************************************************/
-
- /*
- * mixed-model configuration:
- */
- static GLXconfig glx_config[] = {
- {GLX_NORMAL, GLX_DOUBLE, TRUE},
- {GLX_NORMAL, GLX_RGB, TRUE},
- {GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG},
- { 0, 0, 0 },
- };
-
- /** functions ***************************************************************/
-
- /*
- * main - program entry point.
- */
- global void main(
- int argc, /* argument count */
- char *argv[], /* argument vector */
- char *envp[] /* environment pointer */
- )
- {
- XtAppContext app_context; /* application context */
- Display *dsp; /* display ref */
- Widget app_shell; /* first widget */
- Widget form; /* surrounds app */
- Widget rowcol; /* manages input buttons */
- Widget button; /* quit button */
- Widget separator; /* between input and output */
- Widget frame; /* to surround gl widget */
- Widget glw; /* the gl widget inside window */
- Arg args[15]; /* for name/value pairs */
- int n; /* for reusable indices */
-
- /* initialize toolkit, creating application shell */
- n = 0;
- XtSetArg(args[n], XmNtitle, "KB Echo"); n++;
- app_shell = XtAppInitialize(
- &app_context, "Kbecho", NULL, 0, &argc, argv, NULL, args, n
- );
-
- /* create container for app */
- n = 0;
- form = XmCreateForm(app_shell, "form", args, n);
- XtManageChild(form);
-
- /* create the command area */
- n = 0;
- XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
- rowcol = XmCreateRowColumn(form, "rowcol", args, n);
- XtManageChild(rowcol);
-
- /* create the command area buttons */
- n = 0;
- button = XmCreatePushButton(rowcol, "Quit", args, n);
- XtAddCallback(button, XmNactivateCallback, cb_quit, NULL);
- XtManageChild(button);
-
- /* create separator between command area and output area */
- n = 0;
- XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
- XtSetArg(args[n], XmNleftWidget, rowcol); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
- separator = XmCreateSeparator(form, "separator", args, n);
- XtManageChild(separator);
-
- /* create the output area */
- /* create the frame */
- n = 0;
- XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
- XtSetArg(args[n], XmNleftWidget, separator); n++;
- XtSetArg(args[n], XmNleftOffset, 5); n++;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightOffset, 5); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomOffset, 5); n++;
- XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNtopOffset, 5); n++;
- XtSetArg(args[n], XmNshadowThickness, 6); n++;
- frame = XmCreateFrame(form, "frame", args, n);
- XtManageChild(frame);
-
- /* create the gl widget */
- n = 0;
- XtSetArg(args[n], GlxNglxConfig, glx_config); n++;
- XtSetArg(args[n], XmNtraversalOn, True); n++;
- XtSetArg(args[n], XmNborderWidth, 0); n++;
- XtSetArg(args[n], XmNwidth, 400); n++;
- XtSetArg(args[n], XmNheight, 400); n++;
- glw = GlxCreateMDraw(frame, "glw", args, n);
- XtManageChild(glw);
- XtAddCallback(glw, GlxNexposeCallback, cb_gl_expose, 0);
- XtAddCallback(glw, GlxNresizeCallback, cb_gl_resize, 0);
- XtAddCallback(glw, GlxNginitCallback, cb_gl_ginit, 0);
- XtAddCallback(glw, GlxNinputCallback, cb_gl_input, 0);
-
- /* realize the app, creating the actual x windows */
- XtRealizeWidget(app_shell);
- install_colormaps(app_shell, glw);
-
- /* enter the event loop */
- XtAppMainLoop(app_context);
- }
-
-
- /*- support: setup ---------------------------------------------------------*/
- /*
- * install_colormaps - let the window manager know about our colormaps.
- */
- static void install_colormaps(Widget top_level, Widget glw)
- {
- Window overlay_win, popup_win, underlay_win;
- Window window[5];
- int i;
-
- XtVaGetValues(
- glw,
- GlxNoverlayWindow, &overlay_win,
- GlxNpopupWindow, &popup_win,
- GlxNunderlayWindow, &underlay_win,
- NULL
- );
- i = 0;
- if (overlay_win)
- window[i++] = overlay_win;
- if (popup_win)
- window[i++] = popup_win;
- if (underlay_win)
- window[i++] = underlay_win;
- window[i++] = XtWindow(glw);
- window[i++] = XtWindow(top_level);
- XSetWMColormapWindows(XtDisplay(top_level), XtWindow(top_level), window, i);
- }
-
-
- /*- support: callbacks (gl widget) -----------------------------------------*/
- /*
- * cb_gl_expose - handle expose events for the gl widget.
- */
- static void cb_gl_expose(Widget w, XtPointer client_data, XtPointer call_data)
- {
- GlxDrawCallbackStruct *glx = (GlxDrawCallbackStruct *) call_data;
-
- printf("cb_gl_expose()\n");
- GLXwinset(XtDisplay(w), XtWindow(w));
- draw_frame(TRUE, TRUE);
- }
-
-
- /*
- * cb_gl_resize - handle resize events for the gl widget.
- */
- static void cb_gl_resize(Widget w, XtPointer client_data, XtPointer call_data)
- {
- GlxDrawCallbackStruct *glx = (GlxDrawCallbackStruct *) call_data;
-
- printf("cb_gl_resize()\n");
- GLXwinset(XtDisplay(w), XtWindow(w));
- viewport(0, glx->width-1, 0, glx->height-1);
- }
-
-
- /*
- * cb_gl_ginit - perform any necessary graphics initialization.
- */
- static void cb_gl_ginit(Widget w, XtPointer client_data, XtPointer call_data)
- {
- GlxDrawCallbackStruct *glx = (GlxDrawCallbackStruct *) call_data;
-
- printf("cb_gl_ginit()\n");
- GLXwinset(XtDisplay(w), XtWindow(w));
-
- /* add pointer motion events and enter/leave events */
- XSelectInput(
- XtDisplay(w), XtWindow(w), XtBuildEventMask(w) | PointerMotionMask
- );
- XtAugmentTranslations(
- w, XtParseTranslationTable("<MotionNotify>: glxInput()")
- );
-
- mmode(MVIEWING);
- ortho2(0.0, 100.0, 0.0, 100.0);
- gflush();
- }
-
-
- /*
- * cb_gl_input - handle input from a gl window.
- */
- static void cb_gl_input(Widget w, XtPointer client_data, XtPointer call_data)
- {
- GlxDrawCallbackStruct *glx = (GlxDrawCallbackStruct *) call_data;
- XEvent *event;
-
- GLXwinset(XtDisplay(w), XtWindow(w));
- printf("cb_gl_input( ");
- event = glx->event;
- switch (event->type) {
- case ButtonPress:
- printf("ButtonPress (");
- xbutton_print(event);
- printf(")");
- if (event->xbutton.button == Button1) {
- printf("XmProcessTraversal(w, XmTRAVERSE_CURRENT)\n");
- XmProcessTraversal(w, XmTRAVERSE_CURRENT);
- }
- break;
- case ButtonRelease:
- printf("ButtonRelease(");
- xbutton_print(event);
- printf(")");
- break;
- case KeyPress:
- printf("KeyPress (%s)", keycode_to_string(event));
- break;
- case KeyRelease:
- printf("KeyRelease(%s)", keycode_to_string(event));
- break;
- case MotionNotify:
- printf("MotionNotify(%d, %d)", event->xmotion.x, event->xmotion.y);
- break;
- default:
- printf("UnknownEventType(%d)", event->type);
- break;
- }
- printf(" )\n");
- }
-
-
- /*- support: callbacks (misc) ----------------------------------------------*/
- /*
- * cb_quit - exit application.
- */
- static void cb_quit(Widget w, XtPointer client_data, XtPointer call_data)
- {
- exit(0);
- }
-
-
- /*- support: drawing -------------------------------------------------------*/
- /*
- * draw_frame -
- */
- static void draw_frame(Bool do_clear, Bool do_swap)
- {
- static float vert[][2] = { /* a box */
- { 0.0, 0.0},
- {20.0, 0.0},
- {20.0, 20.0},
- { 0.0, 20.0},
- };
-
- if (do_clear) {
- cpack(RGB_BLACK);
- clear();
- }
-
- pushmatrix();
- translate(15.0, 40.0, 0.0);
- cpack(RGB_RED);
- bgnpolygon();
- v2f(vert[0]);
- v2f(vert[1]);
- v2f(vert[2]);
- v2f(vert[3]);
- endpolygon();
- translate(25.0, 0.0, 0.0);
- cpack(RGB_GREEN);
- bgnpolygon();
- v2f(vert[0]);
- v2f(vert[1]);
- v2f(vert[2]);
- v2f(vert[3]);
- endpolygon();
- translate(25.0, 0.0, 0.0);
- cpack(RGB_BLUE);
- bgnpolygon();
- v2f(vert[0]);
- v2f(vert[1]);
- v2f(vert[2]);
- v2f(vert[3]);
- endpolygon();
- popmatrix();
-
- if (do_swap) {
- swapbuffers();
- gflush();
- }
- }
-
-
- /*- support: echoing keys/buttons ------------------------------------------*/
- /*
- * xbutton_print -
- */
- static void xbutton_print(XEvent *event)
- {
- char *str;
-
- switch (event->xbutton.button) {
- case Button1: str = "Button1"; break;
- case Button2: str = "Button2"; break;
- case Button3: str = "Button3"; break;
- case Button4: str = "Button4"; break;
- case Button5: str = "Button5"; break;
- default: str = "Button?"; break;
- }
- printf("%s", str);
- }
-
-
- /*
- * keycode_to_string -
- */
- static char *keycode_to_string(XEvent *event)
- {
- #define KEYSTR_SIZE 100
- static char keystr[KEYSTR_SIZE+1];
- /**/
- KeySym keysym;
- int count;
- char *tmp;
-
- count = XLookupString(
- (XKeyEvent *) event, keystr, KEYSTR_SIZE, &keysym, NULL
- );
- keystr[count] = '\0';
- if (count == 0) {
- tmp = XKeysymToString(keysym);
- strcpy(keystr, tmp!=NULL? tmp : "");
- }
- return (keystr);
- }
-
- /** eof *********************************************************************/
-